import os
import re
from docx import Document

rootdir = './1docx'
lst = os.listdir(rootdir)

ret = {}

for i in range(0,len(lst)):
    tmp_ret = []
    path = os.path.join(rootdir, lst[i])
    if not bool(re.search('^2017', lst[i].split('_')[0])):
        continue
    tmp_ret.append(lst[i].split('_')[0]) # number
    tmp_ret.append(lst[i].split('_')[1]) # name

    if os.path.isfile(path):
        f = open(path, 'rb')
        doc = Document(f)
        tbs = doc.tables
        print(tbs[1])
        print('-------------')
        for idx_row, row in enumerate(tbs[1].rows):
            for idx_cell, cell in enumerate(row.cells):
                # print(idx_row, ':', idx_cell, ':', cell.text)
                if idx_row in list(range(1, 12)) and idx_cell == 3:
                    tmp_ret.append(float(cell.text))
        ret[tmp_ret[0]] = tmp_ret[1:]
        f.close

rootdir = './2docx'
lst = os.listdir(rootdir)

for i in range(0,len(lst)):
    tmp_ret = []
    path = os.path.join(rootdir, lst[i])
    if not bool(re.search('^2017', lst[i].split('_')[0])):
        continue
    tmp_ret.append(lst[i].split('_')[0]) # number

    if os.path.isfile(path):
        f = open(path, 'rb')
        doc = Document(f)
        tbs = doc.tables
        print(tbs[1])
        print('-------------')
        for idx_row, row in enumerate(tbs[1].rows):
            for idx_cell, cell in enumerate(row.cells):
                # print(idx_row, ':', idx_cell, ':', cell.text)
                if idx_row in list(range(1, 7)) and idx_cell == 3:
                    tmp_ret.append(float(cell.text))
        ret[tmp_ret[0]].extend(tmp_ret[1:])
        f.close

rootdir = './3docx'
lst = os.listdir(rootdir)

for i in range(0,len(lst)):
    tmp_ret = []
    path = os.path.join(rootdir, lst[i])
    if not bool(re.search('^2017', lst[i].split('_')[0])):
        continue
    tmp_ret.append(lst[i].split('_')[0]) # number

    if os.path.isfile(path):
        f = open(path, 'rb')
        doc = Document(f)
        tbs = doc.tables
        print(tbs[1])
        print('-------------')
        for idx_row, row in enumerate(tbs[1].rows):
            for idx_cell, cell in enumerate(row.cells):
                # print(idx_row, ':', idx_cell, ':', cell.text)
                if idx_row in list(range(1, 8)) and idx_cell == 3:
                    tmp_ret.append(float(cell.text))
        ret[tmp_ret[0]].extend(tmp_ret[1:])
        f.close


print(len(ret))
for key in ret.keys():
    print(len(ret[key]), key, ret[key])
